package com.wdz.config;



import com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean;
import com.baomidou.mybatisplus.spring.boot.starter.MybatisPlusProperties;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.sql.SQLException;


//@Configuration
//@MapperScan(basePackages = {"com.wdz.tofs.*.mapper"}, sqlSessionTemplateRef = "sqlSessionTemplatey")
public class ResourcessDataSourceConfig {

    @Bean(name = "dataSourcey")
    @ConfigurationProperties(prefix = "spring.datasource.tofs")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "sqlSessionFactoryy")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSourcey") DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        //bean.setConfiguration(mybatisProperties.getConfiguration());
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper2/*/*Mapper.xml"));
        return bean.getObject();
    }

    /*@Bean(name = "transactionManager")
    public DataSourceTransactionManager transactionManager(@Qualifier("dataSourcey") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }*/


    // 其中 dataSource 框架会自动为我们注入
   /* @Bean
    public PlatformTransactionManager txManager(@Qualifier("dataSourcey") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);

    }*/

    @Bean(name = "sqlSessionTemplatey")
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactoryy") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}